package com.company.cloud.base.modules.mgr.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.base.modules.base.entity.RegionInfo;
import com.company.cloud.base.modules.base.service.RegionInfoService;
import com.company.cloud.base.modules.cache.models.RegionModel;
import com.company.cloud.base.modules.mgr.controller.req.RegionInfoQuery4PageParam;
import com.company.cloud.base.modules.mgr.controller.req.RegionInfoQueryMgrParam;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * <p>
 * 地区信息表  Controller
 * </p>
 *
 * @author duzhy
 * @since 2020-05-07
 */
@Authc
@RestController
@RequestMapping("/mgr/base/regionInfo")
@Api(tags="Mgr_Base_地区信息 ")
public class RegionInfoMgrController {

	@Autowired
	private RegionInfoService regionInfoService;

	@PostMapping("/queryPage")
	@ApiOperation(value="分页列表查询", response= RegionModel.class)
	public BaseResult queryPage(@RequestBody RegionInfoQuery4PageParam params) {
		ValidatorUtils.validate(params);
		IPage<RegionInfo> page = regionInfoService.page(new Page<>(params.getPage(), params.getRows()),
				new QueryWrapper<RegionInfo>()
						.eq(StringUtils.hasText(params.getParentCode()), "parent_code", params.getParentCode())
						.eq(StringUtils.hasText(params.getLevel()), "level", params.getLevel())
						.eq(StringUtils.hasText(params.getIsHot()), "is_hot", params.getIsHot())
		);
		IPage<RegionModel> data = page.convert(entity -> {
			RegionModel model = new RegionModel();
			BeanUtil.copyProperties(entity, model);
			return model;
		});

		return BaseResult.ok(data);
	}

	@PostMapping("/queryList")
	@ApiOperation(value="下拉框查询", response= RegionModel.class)
	public BaseResult queryList(@RequestBody RegionInfoQueryMgrParam params) {
		ValidatorUtils.validate(params);
		QueryWrapper<RegionInfo> queryWrapper = new QueryWrapper<RegionInfo>();
		queryWrapper
				.eq(StringUtils.hasText(params.getParentCode()), "parent_code", params.getParentCode());

		List<RegionInfo> list = regionInfoService.list(queryWrapper);
		List<RegionModel> data = new ArrayList<RegionModel>();
		for(RegionInfo regionInfo:list){
			RegionModel model = new RegionModel();
			BeanUtil.copyProperties(regionInfo,model);
			data.add(model);
		}
		return BaseResult.ok(data);
	}
}
